package conf

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

const CONFIG_NAME = "conf.json"

var config *Config = nil

type Config struct {
	Host string
	Port int
}

func GetConfig() *Config {
	return config
}

func (config *Config) Load() error {
	buff, err := ioutil.ReadFile(CONFIG_NAME)
	if err != nil {
		fmt.Println("read conf fail", err)
		return err
	}
	if err := json.Unmarshal(buff, config); err != nil {
		fmt.Println("conf can not match")
		return err
	}
	return nil
}

func init() {
	fmt.Println("start read conf")
	config = new(Config)
	err := config.Load()
	if err != nil {
	} else {
		fmt.Println("read conf success")
	}
}
